package com.wp.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wp.file.common.BaseContext;
import com.wp.file.common.R;
import com.wp.file.pojo.File;
import com.wp.file.pojo.User;
import com.wp.file.pojo.Warehouse;
import com.wp.file.pojo.vo.Audio;
import com.wp.file.pojo.vo.Picture;
import com.wp.file.pojo.vo.Video;
import com.wp.file.service.FileService;
import com.wp.file.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类文件接口
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class ClassifyController {
    @Autowired
    private FileService fileService;
    @Autowired
    private WarehouseService warehouseService;
    @Value("${office.service.storepath}")
    private String STOREPATH;
    @Value("${office.service.onlyurl}")
    private String ONLYURL;
    @Value("${office.service.nginxurl}")
    private String NGINXURL;
    @Value("${office.service.backendurl}")
    private String BACKENDURL;

    @GetMapping("/documents/page")
    public R<Page> pageDoc(int pageNumber, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,pageNumber,pageSize,name);
        //只查个人下的资源

        //构造分页构造器
        Page pageInfo = new Page(pageNumber,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper();
        Long currentId = BaseContext.getCurrentId();
        lqw.eq(Warehouse::getUserId,currentId);
        lqw.eq(Warehouse::getSort,1);//种类1代表文档
        lqw.like(StringUtils.isNotEmpty(name),Warehouse::getDisplayName,name);
        lqw.orderByDesc(Warehouse::getUpdateTime);
        warehouseService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    @GetMapping("/picture/page")
    public R<Page> pagePic(int pageNumber,int pageSize){
        log.info("page = {},pageSize = {}" ,pageNumber,pageSize);
        Page<Warehouse> pageInfo = new Page<>(pageNumber,pageSize);
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        lqw.eq(Warehouse::getUserId,currentId);
        lqw.eq(Warehouse::getSort,2);//种类2代表图片
        lqw.orderByDesc(Warehouse::getUpdateTime);
        warehouseService.page(pageInfo,lqw);
        //图片分页器
        Page<Picture> pagePicInfo = new Page<>();
        //对象拷贝除了records
        BeanUtils.copyProperties(pageInfo,pagePicInfo,"records");
        List<Warehouse> records = pageInfo.getRecords();
        List<Picture> pictureRecords = records.stream().map((item) -> {
            Picture picture = new Picture();
            picture.setId(item.getId());
            picture.setName(item.getDisplayName());
            picture.setUpdateTime(item.getUpdateTime());
            picture.setSrc(giveURL(item));
            return picture;
        }).collect(Collectors.toList());
        pagePicInfo.setRecords(pictureRecords);
        return R.success(pagePicInfo);
    }

    @GetMapping("/audio/page")
    public R<Page> pageAudio(int pageNumber, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,pageNumber,pageSize,name);
        Page<Warehouse> pageInfo = new Page<>(pageNumber,pageSize);
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        lqw.eq(Warehouse::getUserId,currentId);
        lqw.eq(Warehouse::getSort,3);//种类3代表音频
        lqw.like(Warehouse::getDisplayName,name);
        lqw.orderByDesc(Warehouse::getUpdateTime);
        warehouseService.page(pageInfo,lqw);
        //音频分页器
        Page<Audio> pageAudioInfo = new Page<>();
        BeanUtils.copyProperties(pageInfo,pageAudioInfo,"records");
        List<Warehouse> records = pageInfo.getRecords();
        List<Audio> audioRecords = records.stream().map((item) -> {
            Audio audio = new Audio();
            audio.setId(item.getId());
            audio.setName(item.getDisplayName());
            audio.setUpdateTime(item.getUpdateTime());
            audio.setSrc(giveURL(item));
            return audio;
        }).collect(Collectors.toList());
        pageAudioInfo.setRecords(audioRecords);
        return R.success(pageAudioInfo);
    }


    @GetMapping("/video/page")
    public R<Page> pageVideo(int pageNumber,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,pageNumber,pageSize,name);
        Page<Warehouse> pageInfo = new Page<>(pageNumber,pageSize);
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        Long currentId = BaseContext.getCurrentId();
        lqw.eq(Warehouse::getUserId,currentId);
        lqw.eq(Warehouse::getSort,4);//种类4代表视频
        lqw.like(Warehouse::getDisplayName,name);
        lqw.orderByDesc(Warehouse::getUpdateTime);
        warehouseService.page(pageInfo,lqw);
        Page<Video> pageVideoInfo = new Page<>();
        BeanUtils.copyProperties(pageInfo,pageVideoInfo,"records");
        List<Warehouse> records = pageInfo.getRecords();
        List<Video> audioRecords = records.stream().map((item) -> {
            Video video = new Video();
            video.setId(item.getId());
            video.setName(item.getDisplayName());
            video.setUpdateTime(item.getUpdateTime());
            video.setSrc(giveURL(item));
            return video;
        }).collect(Collectors.toList());
        pageVideoInfo.setRecords(audioRecords);
        return R.success(pageVideoInfo);
    }


    /**
     * 根据warehouse给出这个文件的下载地址
     */
    private String giveURL(Warehouse warehouse){
        Long fileId = warehouse.getFileId();
        File file = fileService.getById(fileId);
        String url = NGINXURL+STOREPATH+file.getPath()+"/"+file.getStorename();
        return url;
    }

}
